/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package z.h.w.jar.kit.code.ci;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import z.h.w.jar.kit.method.IO;
import z.h.w.jar.kit.method.Roll;
import z.h.w.jar.kit.method.Self;
import z.h.w.jar.kit.code.pic.Code;
import z.h.w.jar.kit.code.pic.String2Image;

/**
 * 第一种读取图片的方法
 * <p/>
 * File sourceimage = new File("source.gif");
 * <p/>
 * Image image = ImageIO.read(sourceimage);
 * <p/>
 * 第2种读取图片的方法
 * <p/>
 * InputStream is = new BufferedInputStream(new FileInputStream("mid.jpg"));
 * <p/>
 * Image image = ImageIO.read(is);
 * <p/>
 * 第3种读取图片的方法
 * <p/>
 * URL url = new URL("http://www.javaworld.com/images/012407-tipsbox.jpg");
 * <p/>
 * Image image = ImageIO.read(url);
 * <p/>
 * @author EnzoZhong
 */
public class Water implements Code {

      private BufferedImage bufferedImage;
      private Graphics2D g;

      /**
       *
       * @param path
       */
      public Water ( String path ) {
            File file = new File ( path );
            try {
                  Image image = ImageIO.read ( file );
                  Integer width = image.getWidth ( null );
                  Integer height = image.getHeight ( null );
                  bufferedImage = new BufferedImage ( width , height , BufferedImage.TYPE_INT_RGB );
                  bufferedImage.getGraphics ().drawImage ( image , 0 , 0 , null );
                  g = bufferedImage.createGraphics ();
            } catch ( IOException ex ) {
                  Logger.getLogger ( Water.class.getName () ).log ( Level.SEVERE , null , ex );
            }
      }

      /**
       *
       * @param code
       *
       * @return
       */
      public Range calculate ( Code code ) {
            BufferedImage bi = code.getImage ();
            Integer width = bufferedImage.getWidth ();
            Integer height = bufferedImage.getHeight ();
            Integer biWidth = bi.getWidth ();
            Integer biHeight = bi.getHeight ();
            return new Range ( 0 , width - biWidth , 0 , height - biHeight );
      }

      /**
       *
       * @param sub
       */
      public void paste ( BeSub sub ) {
            Point point = sub.getPoint ();
            g.drawImage ( sub.getBufferedImage () , null , point.x , point.y );
      }

      /**
       *
       * @param args
       */
      public static void main ( String[] args ) {
            //Color color, Integer fontSize, Font font, String string
            Water water = new Water ( "C:\\新建文件夹\\foto15.jpg" );
            Code code = new String2Image ( Color.WHITE , 30 , Roll.roll ( Self.getChineseFonts () ) , "www.qwer.so" );

            System.out.println ( water.calculate ( code ) );
            Scanner scan = new Scanner ( System.in );
            System.out.println ( "x值" );
            Integer x = scan.nextInt ();
            System.out.println ( "y值" );
            Integer y = scan.nextInt ();
            Sub sub = new Sub ( code.getImage () , x , y );
            water.paste ( sub );
            IO.bufferedImage2Png ( water.getImage () , "c:\\" + System.currentTimeMillis () + ".png" );
      }

      /**
       *
       * @return
       */
      @Override
      public BufferedImage getImage () {
            return bufferedImage;
      }

      /**
       *
       * @return
       */
      @Override
      public String getString () {
            throw new UnsupportedOperationException ( "Not supported yet." );
      }

      /**
       *
       */
      public class Range {

            private Integer minX;
            private Integer maxX;
            private Integer minY;
            private Integer maxY;

            /**
             *
             * @param minX
             * @param maxX
             * @param minY
             * @param maxY
             */
            public Range ( Integer minX , Integer maxX , Integer minY , Integer maxY ) {
                  this.minX = minX;
                  this.maxX = maxX;
                  this.minY = minY;
                  this.maxY = maxY;
            }

            @Override
            public String toString () {
                  StringBuffer stringBuffer = new StringBuffer ();
                  stringBuffer.append ( minX ).append ( " < x < " ).append ( maxX ).append ( " ; " );
                  stringBuffer.append ( minY ).append ( " < y < " ).append ( maxY );

                  return stringBuffer.toString ();
            }

      }

}
